package order

import (
	"github.com/gin-gonic/gin"
	"github.com/gin-gonic/gin/binding"

	"go-papers/models"
	"go-papers/tools"
	"go-papers/tools/app"
	"go-papers/tools/app/msg"
)

func GetOrderCourseList(c *gin.Context) {
	var data models.OrderCourse
	var err error
	var pageSize = 10
	var pageIndex = 1

	if size := c.Request.FormValue("pageSize"); size != "" {
		pageSize = tools.StrToInt(err, size)
	}
	if index := c.Request.FormValue("pageIndex"); index != "" {
		pageIndex = tools.StrToInt(err, index)
	}

	if index := c.Request.FormValue("orderCourseId"); index != "" {
		data.OrderCourseId = tools.StrToInt(err, index)
	}
	if index := c.Request.FormValue("orderId"); index != "" {
		data.OrderId = tools.StrToInt(err, index)
	}
	if index := c.Request.FormValue("courseId"); index != "" {
		data.CourseId = tools.StrToInt(err, index)
	}

	data.CourseName = c.Request.FormValue("courseName")
	data.CourseCode = c.Request.FormValue("courseCode")
	data.CourseImg = c.Request.FormValue("courseImg")
	data.CourseType = c.Request.FormValue("courseType")
	data.CourseTeach = c.Request.FormValue("courseTeach")
	data.CourseCategory = c.Request.FormValue("courseCategory")
	data.CourseOrganName = c.Request.FormValue("courseOrganName")
	data.CourseBrandName = c.Request.FormValue("courseBrandName")

	data.DataScope = tools.GetUserIdStr(c)
	result, count, err := data.GetPage(pageSize, pageIndex)
	if err != nil {
		app.ApiResp(c, -1, "数据操作失败："+err.Error(), nil)
		return
	}

	app.PageOK(c, result, count, pageIndex, pageSize, "")
}

func GetOrderCourse(c *gin.Context) {
	var data models.OrderCourse
	data.OrderCourseId, _ = tools.StringToInt(c.Param("orderCourseId"))
	result, err := data.Get()
	if err != nil {
		app.ApiResp(c, -2, "抱歉未找到相关信息："+err.Error(), nil)
		return
	}

	app.OK(c, result, "")
}

func InsertOrderCourse(c *gin.Context) {
	var data models.OrderCourse
	err := c.ShouldBindJSON(&data)
	//data.CreateBy = tools.GetUserIdStr(c)
	if err != nil {
		app.ApiResp(c, -1, "数据解析失败："+err.Error(), nil)
		return
	}

	result, err := data.Create()
	if err != nil {
		app.ApiResp(c, -1, "数据操作失败："+err.Error(), nil)
		return
	}

	app.OK(c, result, "")
}

func UpdateOrderCourse(c *gin.Context) {
	var data models.OrderCourse
	err := c.BindWith(&data, binding.JSON)
	if err != nil {
		app.ApiResp(c, -1, "数据解析失败："+err.Error(), nil)
		return
	}

	result, err := data.Update(data.OrderCourseId)
	if err != nil {
		app.ApiResp(c, -1, "数据操作失败："+err.Error(), nil)
		return
	}

	app.OK(c, result, "")
}

func DeleteOrderCourse(c *gin.Context) {
	var data models.OrderCourse

	IDS := tools.IdsStrToIdsIntGroup("orderCourseId", c)
	_, err := data.BatchDelete(IDS)
	if err != nil {
		app.ApiResp(c, -1, "数据删除失败："+err.Error(), nil)
		return
	}

	app.OK(c, nil, msg.DeletedSuccess)
}
